package com.example.messageservice;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import java.net.URL;
import java.net.URLConnection;

/**
 * Created by xuxingcan on 2016/7/31.
 */
public class HttpUtil {
    private static volatile HttpUtil instance;
    private HandlerThread mHandlerThread;
    private Handler mThreadHandler;

    public static HttpUtil getInstance() {
        if (instance == null) {
            synchronized (HttpUtil.class) {
                if (instance == null) {
                    instance = new HttpUtil();
                }
            }
        }
        return instance;
    }

    private HttpUtil() {
        mHandlerThread = new HandlerThread("HttpUtil");
        mHandlerThread.start();
        mThreadHandler = new Handler(mHandlerThread.getLooper());
    }

    public void sendGet(final String webUrl) {
        mThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(webUrl);// 取得资源对象
                    URLConnection uc = url.openConnection();// 生成连接对象
                    uc.connect();// 发出连接
                    long ld = uc.getDate();// 读取网站日期时间
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    URL url = new URL(webUrl);// 取得资源对象
//                    URLConnection uc = url.openConnection();// 生成连接对象
//                    uc.connect();// 发出连接
//                    long ld = uc.getDate();// 读取网站日期时间
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
    }
}
